package com.jingdianjichi.subject.domain.handler.subject;

import com.jingdianjichi.subject.common.enums.SubjectTypeEnum;
import com.jingdianjichi.subject.domain.convert.SubjectConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectBrief;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMultiple;
import com.jingdianjichi.subject.infra.basic.entity.SubjectRadio;
import com.jingdianjichi.subject.infra.basic.service.SubjectBriefService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMultipleService;
import com.jingdianjichi.subject.infra.basic.service.SubjectRadioService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component
@Slf4j
public class MultipleSubjectTypeHandler implements SubjectTypeHandler{


    @Resource
    private SubjectMultipleService subjectMultipleService;
    @Override
    public SubjectTypeEnum getSubjectType() {
        return SubjectTypeEnum.getByCode(SubjectTypeEnum.MULTIPLE.getCode());
    }

    @Override
    public boolean add(SubjectInfoBO subjectInfoBO) {
        List<SubjectAnswerBO> subjectAnswerDTOList = subjectInfoBO.getSubjectAnswerDTOList();
        log.info("MultipleSubjectTypeHandler add subjectAnswerDTOList:{}",subjectAnswerDTOList);
        // 添加空值检查
        if (subjectAnswerDTOList == null || subjectAnswerDTOList.isEmpty()) {
            log.error("多选题答案列表为空");
            return false;
        }
        //bo To  entity
        List<SubjectMultiple> subjectMutipleList = new ArrayList<>();
        subjectAnswerDTOList.forEach(subjectAnswerDTO -> {
            SubjectMultiple subjectMultiple = new SubjectMultiple();
            BeanUtils.copyProperties(subjectAnswerDTO,subjectMultiple);
            subjectMutipleList.add(subjectMultiple);
        });
        boolean flag = subjectMultipleService.saveBatch(subjectMutipleList);
        return flag;
    }

    @Override
    public List<SubjectAnswerBO> query(Long subjectId) {
        List<SubjectMultiple> subjectMultipleList = subjectMultipleService.selectBySubjectId(subjectId);
        if(subjectMultipleList != null && subjectMultipleList.size() > 0){
            List<SubjectAnswerBO> subjectAnswerDTOList = new ArrayList<>();
            subjectMultipleList.forEach(subjectMultiple -> {
                SubjectAnswerBO subjectAnswerBO = new SubjectAnswerBO();
                BeanUtils.copyProperties(subjectMultiple,subjectAnswerBO);
                subjectAnswerDTOList.add(subjectAnswerBO);
            });
            return subjectAnswerDTOList;
        }
        return Collections.emptyList();
    }
}
